﻿using BLL.Sys;
using DEL.Sys;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;

// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace Web.Areas.Sys.Controllers
{
    [Authorize(Roles = "super")]
    public class RoleMenuController : SysController<RoleMenu>
    {

        protected override void SetCurrentEntityService()
        {
            CurrentEntityService = BLLFactory.SetRoleMenuService();
        }

        //根据角色id获取对应的菜单（功能),使用jstree，注意只能返回string集合
        public IActionResult GetMenusByRole(int roleId)
        {
            var roleMenus = CurrentEntityService.GetEntitysByLambda(o => o.RoleId == roleId).ToList();
            var menuIds = from t in roleMenus select t.MenuId.ToString();
            return Json(menuIds.ToList());
        }

        /// <summary>
        /// 更新角色与菜单关联关系
        /// </summary>
        /// <param name="roleId">角色id</param>
        /// <param name="roleMenus">角色权限集合</param>
        /// <returns></returns>
        public virtual IActionResult AddOrModifyMult(int roleId, List<RoleMenu> roleMenus)
        {
            //实现思路为，根据roleId先批量删除现有，再添加            
            CurrentEntityService.DeleteEntitysByLambda(o=>o.RoleId==roleId);
            foreach (var rm in roleMenus) {
                CurrentEntityService.AddEntityAsync(rm);
            }
            
            return Json(new { Result = "Success" });
        }


    }
}
